home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-11-08 | 2.6 KB | 85 lines | [TEXT/MPS ] |
- ############################################################################
- #
- # Name: filename.icn
- #
- # Title: Parse file names
- #
- # Author: Robert J. Alexander
- #
- # Date: December 5, 1989
- #
- ############################################################################
- #
- # suffix(s,separator) -- Parses a hierarchical file name, returning a
- # 2-element list: [prefix,suffix]. E.g. suffix("/a/b/c.d") ->
- # ["/a/b/c","d"]. Separator is the character string that separates
- # the suffix from the rest of the file name, and defaults to ".".
- #
- # tail(s,separator) -- Parses a hierarchical file name, returning a
- # 2-element list: [head,tail]. E.g. tail("/a/b/c.d") ->
- # ["/a/b","c.d"]. Separator is the character string that separates
- # components of the file name, and defaults to the value of environment
- # variable FILESEP, if defined, otherwise "/".
- #
- # components(s,separator) -- Parses a hierarchical file name, returning
- # a list of all directory names in the file path, with the file name
- # (tail) as the last element. E.g. components("/a/b/c.d") ->
- # ["/","a","b","c.d"]. Separator has the same meaning and default
- # value as for tail().
- #
- #
- # Setting the file name component separator
- #
- # There are several ways to set the file name component separator.
- # The normal way is to set up an environment variable on non-UNIX
- # systems so the procedures will work correctly automatically (e.g.
- # SET FILESEP=\ for MS-DOS, or Set FILESEP : ; Export FILESEP for
- # MPW). Other ways are:
- #
- # -- Assign the separator string to global variable
- # "FileSeparator"
- # -- Provide a separator string in the second parameter
- # of the procedure call.
- #
- ############################################################################
-
- global FileSeparator
-
- procedure suffix(s,separator)
- local i
- /separator := "."
- i := *s + 1
- every i := find(separator,s)
- return [s[1:i],s[(*s >= i) + *separator:0] | &null]
- end
-
- procedure tail(s,separator)
- local i
- initial set_separator()
- /separator := FileSeparator
- i := 0
- every i := find(separator,s)
- return case i of {
- 0: [,s]
- 1: [s[1+:*separator],s[1 + *separator:0]]
- default: [s[1:i],s[i + *separator:0]]
- }
- end
-
- procedure components(s,separator)
- local x,head
- initial set_separator()
- /separator := FileSeparator
- x := tail(s,separator)
- return case head := x[1] of {
- separator: [separator]
- &null: []
- default: components(head,separator)
- } ||| ([&null ~=== x[2]] | [])
- end
-
- procedure set_separator()
- initial /FileSeparator := getenv("FILESEP") | "/"
- return
- end
-